package com.wangzhixuan.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.commons.constant.Constants;
import com.wangzhixuan.commons.scan.MyException;
import com.wangzhixuan.commons.utils.LngAndLatUtil;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.commons.utils.date.DateStyle;
import com.wangzhixuan.commons.utils.date.DateUtil;
import com.wangzhixuan.mapper.BaseInfoMapper;
import com.wangzhixuan.mapper.PeopleSignMapper;
import com.wangzhixuan.model.BaseInfo;
import com.wangzhixuan.model.PeopleSign;
import com.wangzhixuan.model.vo.SignVo;
import com.wangzhixuan.service.IPeopleSignService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-07-19
 */
@Service
public class PeopleSignServiceImpl extends ServiceImpl<PeopleSignMapper, PeopleSign> implements IPeopleSignService {
	
	@Autowired
    private PeopleSignMapper peopleSignMapper;
	
	@Autowired
    private BaseInfoMapper baseInfoMapper;
    
	@Override
    public void selectDataGrid(PageInfo pageInfo) {
    	Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<PeopleSign> list = peopleSignMapper.selectPeopleSignPage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

	@Override
	public boolean insertPeopleSign(SignVo signVo) throws MyException {
		BaseInfo baseInfo = baseInfoMapper.selectById(signVo.getWorkId());
		double distance = LngAndLatUtil.getDistance(Double.parseDouble(baseInfo.getLineX()),Double.parseDouble(baseInfo.getLineY()) , Double.parseDouble(signVo.getSignLongitude()), Double.parseDouble(signVo.getSignLatitude()));
		System.out.println(Constants.DISTANCE);
		System.out.println(distance);
		if(Constants.DISTANCE<distance){
			throw new MyException(ApiCode.CODE_1401, "超出签到范围,签到不成功");
		}
		PeopleSign peopleSign = new PeopleSign();
		peopleSign.setUserId(signVo.getUserId());
		peopleSign.setWorkId(signVo.getWorkId());
		peopleSign.setLineX(signVo.getSignLongitude());
		peopleSign.setLineY(signVo.getSignLatitude());
		peopleSign.setAddress(signVo.getSignName());
		peopleSign.setSignTime(new Date());
		peopleSign.setStatus("1");
		peopleSignMapper.insert(peopleSign);
		return true;
	}

	@Override
	public Map<String, Object> selectSignDetail(Integer userId, Integer workId) {
		Map<String,Object> retMap = new HashMap<String,Object>();
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("workId", workId);
		paramMap.put("userId", userId);
		paramMap.put("curDate", DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD));
		String signName = peopleSignMapper.selectCurDateSign(paramMap);
		retMap.put("signName", signName==null?"":signName);
		List<Map<String,Object>> list = peopleSignMapper.selectByWorkIdAndUserId(paramMap);
		retMap.put("signedArr", list);
		return retMap;
	}

	@Override
	public String selectCurDateSign(Map<String, Object> paramMap) {
		return peopleSignMapper.selectCurDateSign(paramMap);
	}
}
